package com.shopping.dev.controller;

import com.shopping.dev.bo.AddressBO;
import com.shopping.dev.bo.CommonResultBO;
import com.shopping.dev.bo.UserBO;
import com.shopping.dev.pojo.UserAddress;
import com.shopping.dev.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;

@Api(value = "收货地址", tags = { "收货地址相关api接口" })
@RestController
@RequestMapping("address")
public class AddressController {

    @Autowired
    private AddressService addressService;
    @ApiOperation(value = "获取用户所有收货地址", notes = "获取用户所有收货地址", httpMethod = "POST", response = CommonResultBO.class)
    @PostMapping("/list")
    public CommonResultBO list(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam String userId) {
        if (StringUtil.isEmpty(userId)){
            return CommonResultBO.error("用户id不能为空");
        }
        List<UserAddress> userAddresses = addressService.queryAll(userId);
        return CommonResultBO.ok(userAddresses);
    }
    @ApiOperation(value = "用户收货地址新增", notes = "用户收货地址新增", httpMethod = "POST", response = CommonResultBO.class)
    @PostMapping("/add")
    public CommonResultBO add(@RequestBody AddressBO addressBO) {
        CommonResultBO commonResultBO = checkAddress(addressBO);
        if (commonResultBO.getStatus()!=200){
            return commonResultBO;
        }
        addressService.addNewUserAddress(addressBO);

        return CommonResultBO.ok();
    }
    private CommonResultBO checkAddress(AddressBO addressBO){

        if (StringUtil.isEmpty(addressBO.getReceiver())){
            return CommonResultBO.error("收货人不能为空");
        }
        if (StringUtil.isEmpty(addressBO.getMobile())){
            return CommonResultBO.error("收货人电话不能为空");
        }

        return CommonResultBO.ok();
    }
    @ApiOperation(value = "修改收货地址", notes = "修改收货地址", httpMethod = "POST", response = CommonResultBO.class)
    @PostMapping("/update")
    public CommonResultBO<UserBO> update(@RequestBody AddressBO addressBO){
        CommonResultBO commonResultBO = checkAddress(addressBO);
        if (commonResultBO.getStatus()!=200){
            return commonResultBO;
        }
        addressService.updateUserAddress(addressBO);

        return CommonResultBO.ok();
    }
    @ApiOperation(value = "删除地址", notes = "删除地址", httpMethod = "POST", response = CommonResultBO.class)
    @PostMapping("/delete")
    public CommonResultBO delete(@RequestParam String userId,@RequestParam String addressId){
        if (StringUtil.isEmpty(userId)||StringUtil.isEmpty(addressId)){
            return CommonResultBO.error("参数错误");
        }
        addressService.deleteUserAddress(userId,addressId);

        return CommonResultBO.ok();

    }

    @ApiOperation(value = "设置默认收货地址", notes = "设置默认收货地址", httpMethod = "POST", response = CommonResultBO.class)
    @PostMapping("/setDefault")
    public CommonResultBO setDefault(@RequestParam String userId,@RequestParam String addressId){
        if (StringUtil.isEmpty(userId)||StringUtil.isEmpty(addressId)){
            return CommonResultBO.error("参数错误");
        }
        addressService.upateUserAddressToDefault(userId,addressId);

        return CommonResultBO.ok();

    }
}
